package entities.forum;

import entities.User;
import entities.reports.ReportedPost;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * Trabalho PD - 2014/2015
 * @author a21210380 a21210392
 */
@Entity
@Table(name = "post")
@NamedQueries({
    @NamedQuery(name = "Post.findAll", query = "SELECT p FROM Post p"),
    @NamedQuery(name = "Post.findByIdPost", query = "SELECT p FROM Post p WHERE p.idPost = :idPost"),
    @NamedQuery(name = "Post.findByMessage", query = "SELECT p FROM Post p WHERE p.message = :message"),
    @NamedQuery(name = "Post.findByCreatedDate", query = "SELECT p FROM Post p WHERE p.createdDate = :createdDate"),
    @NamedQuery(name = "Post.findByIdTopic", query = "SELECT p FROM Post p WHERE p.idTopic = :idTopic"),
    @NamedQuery(name = "Post.findByPostNumber", query = "SELECT p FROM Post p WHERE p.postNumber = :postNumber")})
public class Post implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idPost")
    private Integer idPost;
    
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 700)
    @Column(name = "message")
    private String message;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "createdDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdDate;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "postNumber")
    private int postNumber;
    
    @JoinColumn(name = "idUser", referencedColumnName = "idUser")
    @ManyToOne(optional = false)
    private User idUser;
    
    @JoinColumn(name = "idTopic", referencedColumnName = "idTopic")
    @ManyToOne(optional = false)
    private Topic idTopic;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idPost")
    private Collection<ReportedPost> reports;

    public Post() {
    }
    
    public Post(User idUser, Topic idTopic, String message, Date createdDate, int postNumber) {
        this.idUser = idUser;
        this.idTopic = idTopic;
        this.message = message;
        this.createdDate = createdDate;
        this.postNumber = postNumber;
        this.reports = new ArrayList<>();
    }

    public Integer getId() {
        return idPost;
    }
    
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public int getPostNumber() {
        return postNumber;
    }

    public User getUser() {
        return idUser;
    }

    public Topic getTopic() {
        return idTopic;
    }
    
    public Collection<ReportedPost> getReports() {
        return reports;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idPost != null ? idPost.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Post)) {
            return false;
        }
        Post other = (Post) object;
        return !((this.idPost == null && other.idPost != null) || (this.idPost != null && !this.idPost.equals(other.idPost)));
    }

    @Override
    public String toString() {
        return "entities.extra.Post[ idPost=" + idPost + " ]";
    }

}
